import axios from 'axios'
import config from '@/config.js'

const api = axios.create({
  baseURL: config.apiUrl,
  timeout: 30000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  

  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  console.log('Sending request:', config.method, config.url)

  return config
}, error => {
  console.error('Request interceptor error:', error)
  return Promise.reject(error)
})

// 响应拦截器
api.interceptors.response.use(response => {
  return response
}, error => {
  console.error('Response error:', error)
  return Promise.reject(error)
})

// 获取文件列表
export const getFileList = (params) => {
  return api.get('/file/list', { 
    params:{
        path:params.path || '/',
        type:params.type || 'all'
        } 
    })
}

// 创建文件夹
export const createFolder = (data) => {
  return api.post('/file/createFolder', data)
}

// 删除文件
export const deleteFile = (fileId, fileName) => {
    const config = {
        data: {
            fileName: fileName
        }
    }
    return api.delete(`/file/delete/${fileId}`, config)
}

// 创建分享
export const createShare = (data) => {
  return api.post('/file/share', data)
}

// 获取文件预览
export const getFilePreview = (fileId) => {
  return api.get(`/file/preview/${fileId}`)
}

// 下载文件
export const downloadFile = (fileId) => {
    return api.get(`/file/download/${fileId}`, {
        responseType: 'blob'
    })
}

export const renameFile = (data) => {
    return api.post('/file/rename', data)
}

// 获取回收站列表
export const getTrashList = (params) => {
    return api.get('/file/trash', { 
        params: {
            page: params.page,
            pageSize: params.pageSize
        }
    })
}

// 恢复文件
export const restoreFile = (fileSha1) => {
    return api.post('/file/restore', { fileSha1 })
}
  
// 彻底删除文件
export const permanentlyDeleteFile = (fileSha1,fileName) => {
    return api.post('/file/permanent-delete', { fileSha1,fileName })
}
